from appium import webdriver
from appium.webdriver.common.appiumby import By
from time import sleep

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['app'] = r'e:\SoftWare\apk\zuoyebang.apk'
desired_caps['appPackage'] = 'com.baidu.homework'
desired_caps['appActivity'] = 'com.baidu.homework.activity.init.InitActivity'
desired_caps['noReset'] = False  # False代表重置（重新安装），默认值就是False

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(10)
sleep(3)

# 点击 同意
try:
    ele = driver.find_element(By.ID, 'com.baidu.homework:id/iknow_alert_dialog_button2')
except Exception as e:
    print('no agreeBtn')
else:
    ele.click()

# 点击 同意开启
try:
    ele = driver.find_element(By.ID, 'com.baidu.homework:id/tv_agree_click')
except Exception as e:
    print('no agreeopenBtn')
else:
    ele.click()

# 点击 允许1
try:
    ele = driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button')
except Exception as e:
    print('no allowBtn1')
else:
    ele.click()

# 点击 允许2
try:
    ele = driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button')
except Exception as e:
    print('no allowBtn2')
else:
    ele.click()

# 选择密码登录
driver.find_element(By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget'
                              '.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget'
                              '.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget'
                              '.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget'
                              '.RelativeLayout/android.widget.LinearLayout/android.widget.ImageView').click()
sleep(3)
